package com.service.impl.user;

import com.core.dto.PageDTO;
import com.core.dto.UserDTO;
import com.core.model.user.Role;
import com.core.model.user.User;
import com.core.model.user.UserInfo;
import com.core.model.user.UserRole;
import com.dao.UserDao;
import com.service.user.UserService;
import com.web.handle.RedisTemplateHandle;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: lxf
 * Date: 2016/7/5
 * Time: 15:20
 * To change this template use File | Settings | File Templates.
 */
@Service("userService")
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private RedisTemplateHandle redisTemplateHandle;

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public Map<String, Object> findAllUser(String currentPage) {
        Map<String, Object> map = new HashedMap();
        String totalPage = userDao.findTotalPage(User.class);
        currentPage = PageDTO.isPage(currentPage, totalPage);
        List<User> users = userDao.findAll(User.class, currentPage);
        List<UserDTO> userDTOs = UserDTO.toDTOs(users);
        map.put("userDTOs", userDTOs);
        map.put("currentPage", currentPage);
        map.put("totalPage", totalPage);
        return map;
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public UserDTO findUserByGuid(String userGuid) {
        User user = userDao.findByGuid(User.class, userGuid);
        UserDTO userDTO = UserDTO.toDTO(user);
        return userDTO;
    }

    @Override
    @Transactional
    public boolean saveOrUpdateUser(UserDTO userDTO) {
        try {
            User find = userDao.findIsExistByGuid(User.class, userDTO.getGuid());
            List<Role> roles = userDao.findByGuids(Role.class, userDTO.getRoleGuids());
            List<UserRole> userRoles;
            if (find == null) {
                userRoles = UserDTO.toUserRole(userDTO, roles);
            } else {
                userDao.delUserRoleByUserId(find);
                userRoles = UserDTO.toUserRole(find, userDTO, roles);
            }
            userDao.saveOrUpdate(userRoles);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    @Transactional
    public boolean delUser(String userGuid) {
        try {
            User user = userDao.findIsExistByGuid(User.class, userGuid);
            if (user != null) {
                userDao.delObject(user);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
